package com.qzdsoft.erpcloud.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.utils.StringUtil;

public class BaseController
{
    @Autowired
    private SysUserService sysUserService;
    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    public Integer getUid() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        Cookie cookies[] = request.getCookies();
        if(cookies==null) {
            return null;
        }
        for(Cookie c:cookies) {
            if("uid".equals(c.getName())) {
                return StringUtil.isEmpty(c.getValue())?null:Integer.parseInt(c.getValue());
            }
        }
        logger.error("参数错误，cookie没有传入uid");
        return null;
    }
    
    public LoginUser getUser() {
        return sysUserService.getUser(getUid());
    }
}
